package com.lp.easydraw.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lp.easydraw.R;
import com.lp.easydraw.data.DrawPaintData;
import com.lp.easydraw.data.PaintType;

import java.util.ArrayList;
import java.util.List;

public class PaintAdapter extends RecyclerView.Adapter<PaintAdapter.PaintViewHolder> {
    private static final String TAG = "PaintAdapter";
    private List<DrawPaintData> paintList;
    private ImageButton chooseButton;

    public interface IPaintChoose {
        void choose(DrawPaintData paintData);
    }

    private IPaintChoose iPaintChoose;

    private void initDataList() {
        paintList = new ArrayList<>();
        paintList.add(new DrawPaintData(R.drawable.paint, PaintType.paint));
        paintList.add(new DrawPaintData(R.drawable.brush, PaintType.fillPaint));
        paintList.add(new DrawPaintData(R.drawable.rect, PaintType.rect));
        paintList.add(new DrawPaintData(R.drawable.circle, PaintType.drawCircle));
        paintList.add(new DrawPaintData(R.drawable.elliptic, PaintType.drawOval));
        paintList.add(new DrawPaintData(R.drawable.triangle, PaintType.drawTriangle));
        paintList.add(new DrawPaintData(R.drawable.equilateral_triangle, PaintType.drawEquilateral));
        paintList.add(new DrawPaintData(R.drawable.plan, PaintType.drawRightAngle));
        paintList.add(new DrawPaintData(R.drawable.clear, PaintType.clear));
    }

    public PaintAdapter(IPaintChoose iPaintChoose) {
        initDataList();
        this.iPaintChoose = iPaintChoose;
    }

    @NonNull
    @Override
    public PaintAdapter.PaintViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.paint_item, parent, false);
        PaintViewHolder holder = new PaintViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull PaintAdapter.PaintViewHolder holder, int position) {
        Log.i(TAG, "onBindViewHolder: ");
        holder.imageButton.setImageResource(paintList.get(position).getPaintSrcId());
        holder.imageButton.setOnClickListener(view -> {
            if (chooseButton != null)
                chooseButton.setSelected(false);
            iPaintChoose.choose(paintList.get(position));
            chooseButton = holder.imageButton;
            chooseButton.setSelected(true);
        });
    }

    @Override
    public int getItemCount() {
        return paintList.size();
    }

    public class PaintViewHolder extends RecyclerView.ViewHolder {
        private ImageButton imageButton;

        public PaintViewHolder(@NonNull View itemView) {
            super(itemView);
            imageButton = itemView.findViewById(R.id.ib_paint);

        }

    }
}
